<template>
  <div style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :default-config="toolbarConfig"
      :mode="mode"
    />
    <Editor
      v-model="html"
      style="height: 200px; overflow-y: hidden;"
      :default-config="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>

<script>

import Vue from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
// import { DomEditor } from '@wangeditor/editor'
export default Vue.extend({
  components: { Editor, Toolbar },
  data() {
    return {
      editor: null,
      html: '<p>hello</p>',
      // 工具栏配置
      toolbarConfig: { toolbarKeys: [
        'blockquote', 'header1', 'header2', 'header3',
        '|',
        'bold', 'underline', 'italic', 'through', 'color', 'bgColor', 'clearStyle',
        '|',
        'bulletedList', 'numberedList',
        //  'todo',
        'justifyLeft', 'justifyRight', 'justifyCenter',
        '|',
        'insertLink',
        // 'insertVideo', 'insertTable',
        'codeBlock',
        '|',
        'undo', 'redo'
        // '|',
        // 'fullScreen'
      ] },
      editorConfig: { placeholder: '请输入内容...' },
      // 模式
      mode: 'simple' || 'default' // or 'simple'
    }
  },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    setTimeout(() => {
      this.html = '<p>内容</p>'
    }, 1500)
  },
  updated() {
    // 打印当前工具栏配置
    this.getConfig()
    // 获取编辑器的内容
    // console.log(this.editor.getText())
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  methods: {
    onCreated(editor) {
      //   console.log('editor', editor)
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    // 获取工具栏配置
    getConfig() {
      // const toolbar = DomEditor.getToolbar(this.editor)
      // const curToolbarConfig = toolbar.getConfig()
      // console.log(curToolbarConfig.toolbarKeys)
    }
  }
})
</script>

  <style src="@wangeditor/editor/dist/css/style.css"></style>

  <style>

  </style>
